
package mm.chenchen.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import mm.chenchen.constant.ResultCode;
import mm.chenchen.constant.ResultMsg;

import java.io.Serializable;

/**
 * 返回数据
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private T data;

    public R() {
        code = ResultCode.SUCCESS;
        msg = ResultMsg.SUCCESS;
    }

    public static R error() {
        return error(ResultCode.FAILURE, ResultMsg.FAILURE);
    }

    public static R error(String msg) {
        return error(ResultCode.FAILURE, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.setMsg(msg);
        return r;
    }


    public static R ok() {
        return new R();
    }

    public R setData(T value) {
        this.data = value;
        return this;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }
}
